package org.lq.his.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.lq.his.pojo.Dictionary;
import org.lq.his.pojo.Patient;
import org.lq.his.service.PatientService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/patient")
public class PatientController {
	
	@Resource
	private PatientService patientService;
	
	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping("/index")
	public String selectIndex() throws Exception{
		
		return "patient/patientIndex";
	}
	
	@RequestMapping("/patientfrom")
	public String selectPatientFrom() throws Exception{
		
		return "patient/patientFrom";
	}
	
	
	@RequestMapping("/patientall")
	@ResponseBody
	public List<Patient> selectAll() throws Exception{
		
		return patientService.getAll();
	}
	
	@RequestMapping("/patientList")
	@ResponseBody
	public Map<String, Object> getListByCondition(Integer page, Integer rows,@RequestParam(defaultValue="id") String sort,@RequestParam(defaultValue="asc") String order,Patient condition) throws Exception{
		Map<String, Object> map = new HashMap<>();
		int start = (page - 1) * rows;
		List<Patient> list = patientService.getListByCondition(start, rows, condition, sort, order);
		Integer total = patientService.getCountByCondition(condition);
		
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	@RequestMapping("/patientAdd")
	@ResponseBody
	public Map<String, Object> addPatient(@RequestParam("photo") MultipartFile photo,Patient patient,HttpServletRequest request) throws Exception{
		
		if(!photo.isEmpty()) {
			//保存文件到指定路径
			//获取服务器的绝对路径
			String realPath = request.getServletContext().getRealPath("/img/patientImage");
			System.out.println(realPath);
			String oldName = photo.getOriginalFilename();
			String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
			File dest = new File(realPath+File.separator+newName);
			photo.transferTo(dest);
			patient.setImage("img/patientImage/"+newName);
		}
		
		Map<String, Object> map = new HashMap<>();
		patientService.add(patient);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/selectPatientById")
	@ResponseBody
	public Patient getById(Integer id) throws Exception{
		
		return patientService.getById(id);
	}
	
	@RequestMapping("/patientEdit")
	@ResponseBody
	public Map<String, Object> updatePatient(@RequestParam("photo") MultipartFile photo,Patient patient,HttpServletRequest request) throws Exception{
		
		if(!photo.isEmpty()) {
			//保存文件到指定路径
			//获取服务器的绝对路径
			String realPath = request.getServletContext().getRealPath("/img/patientImage");
			System.out.println(realPath);
			String oldName = photo.getOriginalFilename();
			String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
			File dest = new File(realPath+File.separator+newName);
			photo.transferTo(dest);
			patient.setImage("img/patientImage/"+newName);
		}
		
		Map<String, Object> map = new HashMap<>();
		patientService.update(patient);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/deletePatient")
	@ResponseBody
	public Map<String, Object> bathDeletePatient(Integer[] ids) throws Exception{
		Map<String, Object> map = new HashMap<>();
		patientService.bathDeletePatient(ids);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/EditPatientMoney")
	@ResponseBody
	public Map<String, Object> editPetientMoney(Integer id,Double money) throws Exception{
		Map<String, Object> map = new HashMap<>();
		patientService.editPetientMoney(id, money);
		map.put("result", true);
		return map;
	}
}
